// 成绩

// 解构赋值 拿到 service 中的模块
const {
    getById,  // 根据id获取成绩
    getList,  // 获取成绩列表
    create,   // 创建成绩
    edit,     // 编辑成绩
    remove,   // 删除成绩
} = require("../../service/examination/achievement")

exports.achievementApi = async ctx => {
    // 获取url 字段 继而判断接下来操作
    let url = ctx.request.url.split("/");
    let urlKey = url[2].split("?")[0]
    const { id } = ctx.params || "";
    const { name, pageNum, pageSize, updator, choice, answer, judge, conpletion, testpaper_id, testnumber } = ctx.request.body || "";
    let res;
    // 判断请求方式
    switch (ctx.request.method) {
        // GET 请求时：如果请求路径大于原路径 则根据id获取   反之获取列表数据
        case "GET": if (urlKey == "byid") {
            res = await getById({ id })
        } else if(urlKey == "list"){
            res = await getList({ pageNum, pageSize })
        }; break;
        // POST请求：添加
        case "POST": res = await create({ choice, answer, judge, conpletion, testpaper_id, testnumber, name }); break;
        // DELETE请求： 删除
        case "DELETE": res = await remove({ id }); break;
        // PUT请求： 修改
        case "PUT": res = await edit({ id, updator, choice, answer, judge, conpletion, testpaper_id, testnumber, name }); break;
        default: res = { msg: "操作失败 请求方法不规范", code: "400" }
    }
    ctx.body = res
}